﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;

using XJX.DataLayer;


namespace XJX.BusinessLayer
{
    public class Book
    {
        private string ConnToData;

        public Book(string newConnectionString)
        {
            ConnToData = newConnectionString;
        }

        //添加新书
        public bool Add_New_Book
        (
            string bname,
            string author,
            string press,
            decimal price,
            string isbn,
            string pym,
            string cname,
            string descript,
            string pname,
            string uid,
            out int ReturnValue,
            out string ReturnFieldValue
            )
        {
            bool b;

            try
            {
                b=new XJX.DataLayer.Book(ConnToData).Add_New_Book(bname, author, press, price, isbn, pym, cname, descript, pname, uid, out ReturnValue, out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return b;
        }

        //修改书的信息
        public bool Modify_One_Book_Info
        (
            string bname,
            string author,
            string press,
            decimal price,
            string isbn,
            string pym,
            string cname,
            string descript,
            string pname,
            string uid,
            string bid,
            out int ReturnValue
            )
        {
            bool b;

            try
            {
                b = new XJX.DataLayer.Book(ConnToData).Modify_One_Book_Info(bname, author, press, price, isbn, pym, cname, descript, pname, uid,bid, out ReturnValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return b;
        }



        //得到某位家庭的所有书籍。利用数据表集方式。
        public Hashtable Get_One_Family_Book_From_UID(string UID, int PageIndex, int PageSize)
        {
            Hashtable ht = new Hashtable();
            DataTable dt = new DataTable();
            int PageCount = 0;
            int TotalCount = 0;

            DataSet ds = new XJX.DataLayer.Book(ConnToData).Get_One_Family_Book_From_UID(UID, "Family_Book", PageIndex, PageSize, out PageCount, out TotalCount);
            dt = ds.Tables[0];

            ArrayList data = Common.DataTable2ArrayList(dt);

            ht["PageCount"] = PageCount;//页数,将在哈希表中输出成字符串。
            ht["RecordCount"] = TotalCount;//记录总数
            ht["rows"] = data;

            return ht;
        }

        //得到某位家庭的所有书籍。利用数据表集方式。
        public DataTable Get_One_Family_Book_From_UID(string UID)
        {
            DataTable dt = new DataTable();
            int PageCount = 0;
            int TotalCount = 0;
            int PageIndex = 1;
            int PageSize = 100000;

            DataSet ds = new XJX.DataLayer.Book(ConnToData).Get_One_Family_Book_From_UID(UID, "Family_Book", PageIndex, PageSize, out PageCount, out TotalCount);
            dt = ds.Tables[0];

            return dt;
        }


        ////得到某位家庭的所有书籍。利用只读向前数据方式。
        //public Hashtable Get_One_Family_Book_From_UID_SDR(string UID, int PageIndex, int PageSize)
        //{
        //    Hashtable ht = new Hashtable();

        //    ArrayList data = new XJX.DataLayer.Book(ConnToData).Get_One_Family_Book_From_UID(UID, PageIndex, PageSize);

        //    int pc = (int)Math.Ceiling((double)data.Count / PageSize);

        //    ht["PageCount"] = pc;//页数,将在哈希表中输出成字符串。
        //    ht["RecordCount"] = data.Count;//记录总数
        //    ht["rows"] = data;

        //    return ht;
        //}



        //得到某个分类的图书列表
        public Hashtable Search_Book_List_From_CategoryID(string cname,string UID, int PageIndex, int PageSize)
        {
            Hashtable ht = new Hashtable();
            DataTable dt = new DataTable();
            int PageCount = 0;
            int TotalCount = 0;

            DataSet ds = new XJX.DataLayer.Book(ConnToData).Search_Book_List_From_CategoryID(cname,UID,"Search_Book", PageIndex, PageSize, out PageCount, out TotalCount);
            dt = ds.Tables[0];

            ArrayList data = Common.DataTable2ArrayList(dt);

            ht["PageCount"] = PageCount;//页数,将在哈希表中输出成字符串。
            ht["RecordCount"] = TotalCount;//记录总数
            ht["rows"] = data;

            return ht;
        }

        //查询书名关键字，得到图书列表
        public Hashtable Search_Book_List_From_BookName(string bname,string UID, int PageIndex, int PageSize)
        {
            Hashtable ht = new Hashtable();
            DataTable dt = new DataTable();
            int PageCount = 0;
            int TotalCount = 0;

            DataSet ds = new XJX.DataLayer.Book(ConnToData).Search_Book_List_From_BookName(bname,UID, "Search_Book", PageIndex, PageSize, out PageCount, out TotalCount);
            dt = ds.Tables[0];

            ArrayList data = Common.DataTable2ArrayList(dt);

            ht["PageCount"] = PageCount;//页数,将在哈希表中输出成字符串。
            ht["RecordCount"] = TotalCount;//记录总数
            ht["rows"] = data;

            return ht;
        }


        //得到一本书的相关信息
        public DataTable Get_One_Book_Info_From_BID(string BID)
        {
            DataTable dt = new DataTable();

            DataSet ds = new XJX.DataLayer.Book(ConnToData).Get_One_Book_Info_From_BID(BID);
            dt = ds.Tables[0];

            return dt;
        }


        //下架或上架一本书
        public bool Shelf_One_Book_From_BID
        (
            string uid,
            string bid,
            out int ReturnValue,
            out string ReturnFieldValue
            )
        {
            bool b;

            try
            {
                b = new XJX.DataLayer.Book(ConnToData).Shelf_One_Book_From_BID(uid, bid, out ReturnValue, out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return b;
        }

        //得到某个家庭的下架书
        public Hashtable Get_Off_Shelf_Books_From_UID(string UID, int PageIndex, int PageSize)
        {
            Hashtable ht = new Hashtable();
            DataTable dt = new DataTable();
            int PageCount = 0;
            int TotalCount = 0;

            DataSet ds = new XJX.DataLayer.Book(ConnToData).Get_Off_Shelf_Books_From_UID(UID, "Off_Shelf_Book", PageIndex, PageSize, out PageCount, out TotalCount);
            dt = ds.Tables[0];

            ArrayList data = Common.DataTable2ArrayList(dt);

            ht["PageCount"] = PageCount;//页数,将在哈希表中输出成字符串。
            ht["RecordCount"] = TotalCount;//记录总数
            ht["rows"] = data;

            return ht;
        }

        //得到某个家庭的上架书
        public Hashtable Get_ON_Shelf_Books_From_UID(string UID, int PageIndex, int PageSize)
        {
            Hashtable ht = new Hashtable();
            DataTable dt = new DataTable();
            int PageCount = 0;
            int TotalCount = 0;

            DataSet ds = new XJX.DataLayer.Book(ConnToData).Get_ON_Shelf_Books_From_UID(UID, "ON_Shelf_Book", PageIndex, PageSize, out PageCount, out TotalCount);
            dt = ds.Tables[0];

            ArrayList data = Common.DataTable2ArrayList(dt);

            ht["PageCount"] = PageCount;//页数,将在哈希表中输出成字符串。
            ht["RecordCount"] = TotalCount;//记录总数
            ht["rows"] = data;

            return ht;
        }
    }
}
